<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16
 * Time: 13:46
 */

namespace app\financial\controller;


use app\financial\model\AwardCardModel;
use app\financial\model\AwardStudentModel;
use cmf\controller\AdminBaseController;
use think\Db;

class NiuCardController extends  AdminBaseController
{
    public function index()
    {
        $param=$this->request->param();
        $award_card_model=new AwardCardModel();
        $data=$award_card_model->getList($param);
        $this->assign('data',$data);
        $this->assign('page',$data->render());

        $this->assign('param',$param);
        return $this->fetch();
    }
    public function addPost()
    {
        if($this->request->post())
        {
            $post=$this->request->post();
            $card_type=$post['card_type'];
            if(!is_numeric($post['card_number']))
            {
                $this->error('请输入卡号是一串数字');
            }
            if(!is_numeric($post['award_money']))
            {
                $this->error('请输入金额是一串数字');
            }
            if($post['award_money']<0)
            {
                $this->error('请输入牛牛卡金额大于0');
            }
            if(empty($card_type))
            {
                $this->error('没有选择类型');
            }
            if(intval($card_type)==1)
            {
                unset($post['valid_date']);

            }
            $award_card_model=new AwardCardModel();
            $award_student_model=new AwardStudentModel();
            //卡号是否存在
            $number=$award_card_model->where(['card_number' => trim($post['card_number'])])->count();
            if(isset($number) && $number>0)
            {
                $this->error('卡号存在了');
            }
            if(intval($card_type)==2)
            {
                unset($post['card_number']);
                if(empty($post['valid_date']))
                {
                    $this->error('有效日期为空');
                }
                $valid_date=$post['valid_date'];
                unset($post['valid_date']);
                $post['valid_date']=strtotime($valid_date);
            }
            $post['created_time']=time();
            $p_id=cmf_get_current_admin_id();
            $post['create_card_user_id']=$p_id;
            $user_data=Db::name('user')->where('id',$p_id)->find();

            $insert_id=$award_card_model->insertGetId($post);
            if($insert_id)
            {
                $data=['award_id'=>$insert_id];
                $award_student_model->insert($data);

                //记录日志
                $pk = $insert_id; //主键
                $table = 'award_card';//数据表名
                $content =  $user_data['user_login'].'添加一张'.$post['card_number'].'牛牛卡';
                $id=do_log($content, $pk, $table, time());
                $this->success('添加信息成功',url('NiuCard/index'));
            }else{
                $this->error('添加信息失败');
            }
        }

    }
    public function delete()
    {
        $ids = $this->request->param('ids/a');
        $id = $this->request->param('id', 0, 'intval');
        $award_card_model=new AwardCardModel();
        $award_student_model=new AwardStudentModel();
        $p_id=cmf_get_current_admin_id();
        $user_login=Db::name('user')->where('id',$p_id)->value('user_login');//当前用户名字

        if (!is_array($ids)) {
                $res = $award_card_model->where('id', intval($id))->delete();
                 $card_number= $award_card_model->where('id', intval($id))->value('card_number');//卡号
                 $student_id=$award_student_model->where('award_id', intval($id))->value('student_id');
                  $award_student_model->where('award_id', intval($id))->delete();

            if($res)
            {

                //记录日志
                $pk = $id; //主键
                $table = 'award_card';//数据表名
                if(empty($card_number))
                {
                    if(!empty($student_id))
                    {
                        $name=Db::name('student')->where(['id'=>$student_id])->value('name');

                    }else{
                        $name='';
                    }
                    $content = $user_login .'删除'.$name.'奖学金';

                }else{
                    $content = $user_login .'删除一张['.$card_number.']牛牛卡';
                }
                $id=do_log($content, $pk, $table, time());

                $this->success('删除信息成功',url('NiuCard/index'));
            }else{
                $this->error('删除信息失败');
            }
        }
        if(is_array($ids)) {
            $res= $award_card_model->where(['id' => ['in', $ids]])->delete();
            $award_student_model->where(['award_id' => ['in', $ids]])->delete();
            if($res)
            {


                //记录日志
                $pk = implode(",",$ids); //主键
                $table = 'award_card';//数据表名
                $content = $user_login .'批量删除牛牛卡';
                $id=do_log($content, $pk, $table, time());

                $this->success('删除信息成功',url('NiuCard/index'));

            }else{
                $this->error('删除信息失败');
            }
        }

    }

    /**
     *后台用户绑定家长用户--只有牛牛卡
     */
    public function editPost()
    {
        $param=$this->request->param();
        $student_id=$param['student_id'];
        $id=$param['id'];
        if(empty($id) || empty($student_id))
        {
            $this->error('没有此用户');
        }
        $id=Db::name('award_student')->where('award_id',$id)->update(['is_used'=>1,'u_id'=>$student_id]);
        if($id)
        {
            $this->success('绑定成功');
        }else{
            $this->error('绑定失败');
        }
    }

    /**
     *查询家长用户
     */
    public function ajax_user()
    {
        $param=$this->request->param();
//        15053226646
        $mobile=$param['mobile'];
        $pid=Db::name('user')->where(['mobile'=>$mobile])->value('id');
        if(empty($pid))
        {
            return 0;
        }
        $student_model=Db::name('student')->where('u_id',$pid)->select();
        if($student_model->isEmpty())
        {
            return 0;
        }else{
            return json_encode($student_model);
        }
    }

}